<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/16
 * Time: 14:23
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Dealer extends Base {
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['dealer_name|dealer_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($province)&&$province!="")
            {
                $map['dealer_sheng'] = $province;
            }
            if(isset($city)&&$city!="")
            {
                $map['dealer_shi'] = $city;
            }
            if(isset($district)&&$district!="")
            {
                $map['dealer_qu'] = $district;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('dealer')
                ->join('up_car_series', 'cs_id = dealer_car_id', 'left')
                ->where($map)
                ->count();// 获取总条数
            $lists = Db::name('dealer')
                ->join('up_car_series', 'cs_id = dealer_car_id', 'left')
                ->join('up_area ua1', 'ua1.district_id = dealer_sheng', 'LEFT')
                ->join('up_area ua2', 'ua2.district_id = dealer_shi', 'LEFT')
                ->join('up_area ua3', 'ua3.district_id = dealer_qu', 'LEFT')
                ->field('dealer_id, dealer_name, dealer_phone, cs_name, dealer_address, ua1.district sheng, 
                ua2.district shi, ua3.district qu')
                ->where($map)
                ->page($NowPage,$limits)
                ->select();
            
            foreach ($lists as $key => $vo){
                $lists[$key]['address'] = $vo['sheng'] . $vo['shi'] . $vo['qu'];
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $province = Db::table('up_area')->where('level', 1)->select();
        $this->assign([
            'province' => $province,
        ]);
        return $this->fetch();
    }
    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('dealer')->where('dealer_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /****
     * 添加
     * @return mixed
     */
    public function add(){
        if(Request::instance()->isPost()){
            $dealer_name =input('param.dealer_name');
            $dealer_phone = input('param.dealer_phone');
            $dealer_sheng= input('param.province');
            $dealer_shi= input('param.city');
            $dealer_qu = input('param.district');
            $dealer_address = input('param.activity_address');
            $dealer_lon = input('param.longitude');
            $dealer_lat = input('param.latitude');
            $dealer_car_id = input('param.dealer_car_id');
            if($dealer_address == '' || $dealer_lon == '' || $dealer_lat == ''){
                return json(['code' => 100, 'msg' => '请填写经销商地址']);
            }
            $data = [
                'dealer_name' => $dealer_name,
                'dealer_phone' => $dealer_phone,
                'dealer_sheng' => $dealer_sheng,
                'dealer_shi' => $dealer_shi,
                'dealer_qu'=>$dealer_qu,
                'dealer_address'=>$dealer_address,
                'dealer_lon'=>$dealer_lon,
                'dealer_lat'=>$dealer_lat,
                'dealer_car_id' => $dealer_car_id
            ];
            $res = Db::table('up_dealer')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        // 车系
        $series = Db::table('up_car_series')->field('cs_id, cs_name')->order('cs_time desc')->select();
        $province = Db::table('up_area')->where('level', 1)->select();
        $this->assign([
            'province' => $province,
            'series' => $series
        ]);
        return $this->fetch();
    }

    /****
     * 编辑
     * @return mixed
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $dealer_name =input('param.dealer_name');
            $dealer_phone = input('param.dealer_phone');
            $dealer_sheng= input('param.province');
            $dealer_shi= input('param.city');
            $dealer_qu = input('param.district');
            $dealer_address = input('param.activity_address');
            $dealer_lon = input('param.longitude');
            $dealer_lat = input('param.latitude');
            $dealer_car_id = input('param.dealer_car_id');
            if($dealer_address == '' || $dealer_lon == '' || $dealer_lat == ''){
                return json(['code' => 100, 'msg' => '请填写经销商地址']);
            }
            $data = [
                'dealer_name' => $dealer_name,
                'dealer_phone' => $dealer_phone,
                'dealer_sheng' => $dealer_sheng,
                'dealer_shi' => $dealer_shi,
                'dealer_qu'=>$dealer_qu,
                'dealer_address'=>$dealer_address,
                'dealer_lon'=>$dealer_lon,
                'dealer_lat'=>$dealer_lat,
                'dealer_car_id' => $dealer_car_id
            ];
            $res = Db::table('up_dealer')->where('dealer_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('param.dealer_id');

        $info = Db::table('up_dealer')->where('dealer_id',$id)->find();
        $city = Db::table('up_area')->where('pid',$info['dealer_sheng'])->select();
        $qu = Db::table('up_area')->where('pid',$info['dealer_shi'])->select();
        $province = Db::table('up_area')->where('level', 1)->select();
        // 车系
        $series = Db::table('up_car_series')->field('cs_id, cs_name')->order('cs_time desc')->select();
        $this->assign([
            'province' => $province,
            'id'=>$id,
            'info'=>$info,
            'city'=>$city,
            'qu'=>$qu,
            'series' => $series
        ]);
        return $this->fetch();
    }
}